// packageA/pages/testA/testA.js
Page({
  data: {
    tempFilePath: ''
  },
  // 手机号快速验证
  getPhoneNumber(event) {
    const code = event.detail.code
    wx.request({
      url: 'http://127.0.0.1:5000/wx/get_access_token',
      method: "POST",
      data: {
        code
      },
      success(res) {
        const access_token = res.data.access_token
        wx.request({
          url: 'http://127.0.0.1:5000/wx/getPhoneNumber',
          method: "POST",
          data: {
            code,
            access_token
          },
          success(res) {
            console.log(res.data.phone_info.phoneNumber)
          }
        })
      },
    })
  },
  // 手机号实时验证
  getrealtimephonenumber(event) {
    console.log(event)
  },

  // 一键登录
  getOpenId() {
    wx.login({
      success: (res) => {
        const code = res.code
        wx.request({
          url: 'http://127.0.0.1:5000/wx/getOpenid',
          method: "POST",
          data: {
            code
          },
          success(res) {
            let {
              data
            } = res
            if (data.code === '00000') {
              wx.showToast({
                title: data.msg,
              })
            }else{
              wx.showToast({
                title: '登录失败',
                icon: 'error'
              })
            }
          }
        })
      },
    })
  },
  // 获取用户昵称
  nicknameChange(event) {
    console.log('nicknameChange', event.detail.value)
  },
  // 获取头像
  onChooseAvatar(event) {
    this.setData({
      // 也可以将路径上传给服务器
      avatar: event.detail.avatarUrl
    })
  },
  // 转发给好友
  onShareAppMessage(obj) {
    console.log(obj.from); // 分享的来源
    return {
      title: '分享的标题',
      imageUrl: "/assets/images/bby.png",
      path: "/pages/user/user"
    }
  },
  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '帮我看一刀',
      query: "userid=5",
      imageUrl: "/assets/images/bby.png"
    }
  },
  // 主要的选择媒体函数
  async selectMedia() {
    try {
      // 选择图片
      const mediaRes = await this.chooseMediaPromise();
      const tempFilePath = mediaRes.tempFiles[0].tempFilePath;

      this.setData({
        tempFilePath
      });

      // 检查权限
      const settingRes = await this.checkAuthPromise();

      if (!settingRes.authSetting["scope.writePhotosAlbum"]) {
        // 用户没有权限，显示确认框
        const modalRes = await new Promise((resolve) => {
          wx.showModal({
            title: "提示",
            content: "检测你没有存相册权限，是否去设置打开",
            showCancel: false,
            success: (res) => resolve(res)
          });
        });

        if (modalRes.confirm) {
          try {
            // 尝试获取权限
            await this.requestAuthPromise();
            await this.saveImagePromise(tempFilePath);
            wx.showToast({
              title: '保存成功',
              icon: 'success'
            });
          } catch (authErr) {
            // 如果获取权限失败，打开设置页面
            const settingModalRes = await new Promise((resolve) => {
              wx.showModal({
                title: "权限",
                content: "打开设置页面",
                showCancel: false,
                success: (res) => resolve(res)
              });
            });

            if (settingModalRes.confirm) {
              const settingRes = await this.openSettingPromise();
              if (settingRes.authSetting["scope.writePhotosAlbum"]) {
                await this.saveImagePromise(tempFilePath);
                wx.showToast({
                  title: '保存成功',
                  icon: 'success'
                });
              }
            }
          }
        }
      } else {
        // 用户已有权限，直接保存
        await this.saveImagePromise(tempFilePath);
        wx.showToast({
          title: '保存成功',
          icon: 'success'
        });
      }
    } catch (error) {
      console.error('操作失败：', error);
      // 如果是用户取消，不显示错误提示
      if (error.errMsg && !error.errMsg.includes('cancel')) {
        wx.showToast({
          title: '操作失败',
          icon: 'error'
        });
      }
    }
  },

})